package sh.wn36.vehiclemanage.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import sh.wn36.vehiclemanage.entity.AccidentRecord;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import sh.wn36.vehiclemanage.entity.dto.AccidentQuery;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author xk
 * @since 2021-04-26
 */
@Repository
public interface AccidentRecordMapper extends BaseMapper<AccidentRecord> {
    @Select("  SELECT" +
            "    accident_record.id," +
            "    car.car_no," +
            "    driver.name," +
            "    accident_record.acc_date," +
            "    accident_record.acc_place," +
            "    accident_record.acc_explain," +
            "    accident_record.result," +
            "    accident_record.we_situation," +
            "    accident_record.other_situation," +
            "    accident_record.we_amount," +
            "    accident_record.other_amount," +
            "    accident_record.ins_amount," +
            "    accident_record.remarks" +
            "    FROM accident_record left join car on accident_record.car_id=car.id " +
            "    left join driver on accident_record.driver_id=driver.id" +
            "    ${ew.customSqlSegment} ")
    List<Map<String,Object>> selectAccidentRecordMap
            (Page<Map<String,Object>> page,
             @Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("SELECT \n" +
            "car.car_no ,\n" +
            "IFNULL(SUM(accident_record.`we_amount`-accident_record.ins_amount),0) AS car_expenses,\n" +
            "\"事故记录表\" AS '数据来源'\n" +
            "FROM \n" +
            "car LEFT JOIN accident_record ON accident_record.car_id = car.id \n" +
            "GROUP BY car.`car_no` " +
            "HAVING car_expenses!=0")
    List<Map<String, Object>> reportList();

    @Select("SELECT \n" +
            "car.car_no ,\n" +
            "IFNULL(SUM(accident_record.`we_amount`-accident_record.ins_amount),0) AS car_expenses,\n" +
            "\"事故记录表\" AS '数据来源'\n" +
            "FROM \n" +
            "car LEFT JOIN accident_record ON accident_record.car_id = car.id \n" +
            "where car.id=#{carId} "+
            "GROUP BY car.`car_no` " +
            "HAVING car_expenses!=0 ")
    List<Map<String, Object>> queryReportByCarId(Integer carId);
}
